package com.ruoyi.cases.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cases.utils.excel.ExcelExportToFileUrlComplex;
import com.ruoyi.cases.utils.excel.HexBeanUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cases.domain.JlTroubleshootingTable;
import com.ruoyi.cases.service.IJlTroubleshootingTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 病害排查Controller
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@RestController
@RequestMapping("/cases/troubleshootingTable")
public class JlTroubleshootingTableController extends BaseController
{
    @Autowired
    private IJlTroubleshootingTableService jlTroubleshootingTableService;

    /**
     * 查询病害排查列表
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:list')")
    @GetMapping("/list")
    public TableDataInfo list(JlTroubleshootingTable jlTroubleshootingTable)
    {
        startPage();
        List<JlTroubleshootingTable> list = jlTroubleshootingTableService.selectJlTroubleshootingTableList(jlTroubleshootingTable);
        return getDataTable(list);
    }

    /**
     * 导出病害排查列表
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:export')")
    @Log(title = "病害排查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JlTroubleshootingTable jlTroubleshootingTable)
    {
        List<JlTroubleshootingTable> list = jlTroubleshootingTableService.selectJlTroubleshootingTableList(jlTroubleshootingTable);
        ExcelUtil<JlTroubleshootingTable> util = new ExcelUtil<JlTroubleshootingTable>(JlTroubleshootingTable.class);
        util.exportExcel(response, list, "病害排查数据");
    }

    /**
     * 获取病害排查详细信息
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(jlTroubleshootingTableService.selectJlTroubleshootingTableById(id));
    }

    /**
     * 新增病害排查
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:add')")
    @Log(title = "病害排查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JlTroubleshootingTable jlTroubleshootingTable)
    {
        return toAjax(jlTroubleshootingTableService.insertJlTroubleshootingTable(jlTroubleshootingTable));
    }

    /**
     * 新增病害排查
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:add')")
    @Log(title = "病害排查获取审核人信息", businessType = BusinessType.INSERT)
    @PostMapping("/getAuditMsg")
    public AjaxResult getAuditMsg(String deptId)
    {
        return success(jlTroubleshootingTableService.getAuditMsg(deptId));
    }

    /**
     * 修改病害排查
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:edit')")
    @Log(title = "病害排查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JlTroubleshootingTable jlTroubleshootingTable)
    {
        if(jlTroubleshootingTableService.updateJlTroubleshootingTable(jlTroubleshootingTable)>0){
            return AjaxResult.success("操作成功", null);
        }else{
            return AjaxResult.error("操作失败：非发起人无法操作当前数据！", null);
        }
    }

    /**
     * 删除病害排查
     */
    @PreAuthorize("@ss.hasPermi('cases:troubleshootingTable:remove')")
    @Log(title = "病害排查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        if(jlTroubleshootingTableService.deleteJlTroubleshootingTableByIds(ids)>0){
            return AjaxResult.success("操作成功", null);
        }else{
            return AjaxResult.error("操作失败：非发起人无法操作当前数据！", null);
        }
    }


    /**
     * 导出病害排查列表
     */
    @PostMapping("/exportTest")
    public void exportTest(HttpServletRequest request,HttpServletResponse response, JlTroubleshootingTable jlTroubleshootingTable)
    {
        List<JlTroubleshootingTable> list = jlTroubleshootingTableService.selectJlTroubleshootingTableList(jlTroubleshootingTable);
//        ExcelUtil<JlTroubleshootingTable> util = new ExcelUtil<JlTroubleshootingTable>(JlTroubleshootingTable.class);
//        util.exportExcel(response, list, "病害排查数据");
        for(JlTroubleshootingTable val: list){
            val.setDateStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_CHI, val.getUploadTime()));
        }
    try{
        int xh = 1;
        Map model = new HashMap();
        List<Map<String, Object>> listdata = new ArrayList<>();
        for (JlTroubleshootingTable val: list) {
            val.setXh(xh);
            Map<String, Object> map = HexBeanUtils.object2Map(val);
            listdata.add(map);
            xh++;
        }
        String name ="xh,dateStr,deptName,deptName,deptName,deptName,deptName,deptName,deptName,deptName";
        model.put("excelTitleClos", name.split(",").length-1);
        model.put("fileName", "车道巡检明细表");
        model.put("excelTitle", "张三养护工区定额工作完成情况评价表（个人）");
        //[[行号,列号(&分隔),文字(&分隔),合并单元格(起始行,结束行,起始列,结束列)]]
        model.put("names", name.split(","));
        model.put("excelTitleFontSize", "20");
        model.put("titles0",
                new String[]{"张三养护工区定额工作完成情况评价表（个人）"});
        model.put("titles1",
                new String[]{"序号", "日期", "工区", "姓名", "工作量明细","额定系数","定额标准","奖励/扣除金额","考核人","备注"});
        model.put("titleStarRows", 1);
        model.put("titleRows", 2);
        model.put("list", listdata);
        String columnWidth =
                "0,15&1,30&2,20&3,20&4,25&5,25&6,25&7,25&8,25&9,50";
        model.put("columnWidth", columnWidth);
        new ExcelExportToFileUrlComplex().buildExcelDocument1(model, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
